package com.ruoyi.lucky.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lucky.mapper.TBoxGoodsMapper;
import com.ruoyi.lucky.domain.TBoxGoods;
import com.ruoyi.lucky.service.ITBoxGoodsService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 盲盒商品关联表Service业务层处理
 * 
 * @author liuhongbin
 * @date 2020-11-15
 */
@Service
public class TBoxGoodsServiceImpl implements ITBoxGoodsService 
{
    @Autowired
    private TBoxGoodsMapper tBoxGoodsMapper;

    /**
     * 查询盲盒商品关联表
     * 
     * @param id 盲盒商品关联表ID
     * @return 盲盒商品关联表
     */
    @Override
    public TBoxGoods selectTBoxGoodsById(Integer id)
    {
        return tBoxGoodsMapper.selectTBoxGoodsById(id);
    }

    /**
     * 查询盲盒商品关联表列表
     * 
     * @param tBoxGoods 盲盒商品关联表
     * @return 盲盒商品关联表
     */
    @Override
    public List<TBoxGoods> selectTBoxGoodsList(TBoxGoods tBoxGoods)
    {
        return tBoxGoodsMapper.selectTBoxGoodsList(tBoxGoods);
    }

    /**
     * 新增盲盒商品关联表
     * 
     * @param tBoxGoods 盲盒商品关联表
     * @return 结果
     */
    @Override
    public int insertTBoxGoods(TBoxGoods tBoxGoods)
    {
        tBoxGoods.setCreateTime(DateUtils.getNowDate());
        return tBoxGoodsMapper.insertTBoxGoods(tBoxGoods);
    }

    /**
     * 修改盲盒商品关联表
     * 
     * @param tBoxGoods 盲盒商品关联表
     * @return 结果
     */
    @Override
    public int updateTBoxGoods(TBoxGoods tBoxGoods)
    {
        tBoxGoods.setUpdateTime(DateUtils.getNowDate());
        return tBoxGoodsMapper.updateTBoxGoods(tBoxGoods);
    }

    /**
     * 删除盲盒商品关联表对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTBoxGoodsByIds(String ids)
    {
        return tBoxGoodsMapper.deleteTBoxGoodsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除盲盒商品关联表信息
     * 
     * @param id 盲盒商品关联表ID
     * @return 结果
     */
    @Override
    public int deleteTBoxGoodsById(Integer id)
    {
        return tBoxGoodsMapper.deleteTBoxGoodsById(id);
    }

    /**
     * 盲盒添加商品
     * **/
    /**
     * 成员组成员修改
     * **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateGoodsByBox(String idList,Long blindboxId) throws Exception
    {
        String message = "添加成功";
        TBoxGoods tBoxGoods=new TBoxGoods();
        tBoxGoods.setBlindboxId(blindboxId);
        List<TBoxGoods> tBoxGoodsList =tBoxGoodsMapper.selectTBoxGoodsList(tBoxGoods);
        String[] ids=new String[tBoxGoodsList.size()];
        if(!CollectionUtils.isEmpty(tBoxGoodsList)){
            int i=0;
            for(TBoxGoods gondsBox:tBoxGoodsList){
                ids[i] = gondsBox.getGoodsId().toString();
                i = i+1;
            }
        }
        //新增已选中商品
        String[] idArrary = idList.split(",");
        int addNum = 0;
        List<TBoxGoods> goodsList=new ArrayList<>();
        for (String id : idArrary) {
            if(!Arrays.asList(ids).contains(id)) {
                TBoxGoods boxGoods = new TBoxGoods();
                boxGoods.setBlindboxId(blindboxId);
                boxGoods.setGoodsId(Long.parseLong(id));
                goodsList.add(boxGoods);
            }
        }
        //批量插入
        if(!CollectionUtils.isEmpty(goodsList)) {
            for(TBoxGoods tBoxGoods1:goodsList){
                tBoxGoodsMapper.insertTBoxGoods(tBoxGoods1);
            }
            if (addNum < 0) {
                throw new SQLException("添加失败..");
            }
        }
        return message;
    }
}
